package pulpfizz.pulp;

import pulpcore.Stage;
import pulpcore.image.Colors;
import pulpcore.scene.Scene2D;
import pulpcore.sprite.*;

/**
 * An extension to Scene2D that creates an underlying CameraSprite. All objects added to this
 * scene will be added to the camera layer, and you can access and control the camera using the public field.
 * @author Greg
 *
 */
public class CameraControlledScene extends Scene2D
{
	public CameraSprite camera;

    @Override
	public void load()
	{
		super.load();
		FilledSprite bg = new FilledSprite(Colors.BLACK);
		super.add(bg);
		Group trueBackground = super.getMainLayer();
		camera = new CameraSprite();
        trueBackground.add(camera);
	}

	@Override
	public synchronized void add(Sprite sprite)
	{
		if (camera != null)
			camera.add(sprite);
		else
			super.add(sprite);
	}
	
	/**
	 * Adds a sprite in a fixed location despite camera
	 * movement.
	 * @param sprite
	 */
	public synchronized void addToHUD(Sprite sprite) {
		super.getMainLayer().add(sprite);
	}

    /**
     * Removes a sprite from the HUD.
     * @param sprite
     */
    public synchronized void removeFromHUD(Sprite sprite) {
        super.getMainLayer().remove(sprite);
    }

    @Override
	public synchronized void addLayer(Group layer)
	{
		if (camera != null)
			camera.add(layer);
		else
			super.addLayer(layer);
	}

	@Override
	public synchronized Group getMainLayer()
	{
		if (camera != null)
			return camera;
		else
			return super.getMainLayer();
	}

	public synchronized Group getBottomLayer()
	{
		return super.getMainLayer();
	}
	
}
